package LeetcodeTest_seven;
//三角形的最大周长
import java.util.Arrays;

public class largestPerimeter
{
    public static void main(String[] args)
    {
        int A[]={3,2,3,4};
        int result=Solution.largestPerimeter(A);
        System.out.println(result);
    }
}
/*
class lengthOfLastWord
{
    public static  int maxLength=0;
    public static int largestPerimeter(int[] A)
    {
        int result;
        ArrayList<Integer> answer=new ArrayList<Integer>();
        backtrace(0,answer,A);
        result=maxLength;
        maxLength=0;
        return result;
    }
    public static void backtrace(int index,ArrayList<Integer> num,int[] A)
    {
        if(num.size()==3)
        {
            if(isTriangle(num))
            {
                int sum=num.get(0)+num.get(1)+num.get(2);
                if (maxLength <sum)
                    maxLength=sum;
            }
        }
        else
        {
            for (int i = index; i < A.length; i++)
            {
                num.add(A[i]);
                backtrace(i+1,num, A);
                num.remove(num.size() - 1);
            }
        }
    }
    public static boolean isTriangle(ArrayList<Integer> length)
    {
        if(length.get(0)<length.get(1))
            return (length.get(0)+length.get(1))>length.get(2) && (length.get(1)-length.get(0))<length.get(2);
        else
            return (length.get(0)+length.get(1))>length.get(2) && (length.get(0)-length.get(1))<length.get(2);
    }
}*/
class Solution {
    public static int largestPerimeter(int[] A)
    {
        Solution.chooseSort(A);
        //用Arrays.sort(A)更快
        for(int i=A.length-3;i>=0;--i)
        {
            if(A[i]+A[i+1]>A[i+2])
                return A[i]+A[i+1]+A[i+2];
        }
        return 0;
    }
    public static void chooseSort(int A[])
    {
        int minIndex,temp;
        for(int i=0;i<A.length-1;i++)
        {
            minIndex=i;
            for (int j = i + 1; j < A.length; j++)
            {
                if(A[minIndex]>A[j])
                    minIndex=j;
            }
            temp=A[minIndex];
            A[minIndex]=A[i];
            A[i]=temp;
        }
    }
}